package assists
{
	import assists.log.Log;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.geom.Point;
	
	import main.ClientConstants;

	public class SizeManager
	{
		public static var DPI:int;
		private static var _stage:Stage;
		private static var _distance1:int;
		private static var _distance2:int;
		private static var _distance3:int;
		private static var _distance4:int;
		
		public static function init(stage:Stage):void
		{
			trace("舞台宽度:" + stage.stageWidth + "   舞台高度:" + stage.stageHeight);
			_stage = stage;
			_distance1 = _stage.stageHeight * 0.02;
			_distance3 = _stage.stageHeight * ClientConstants.HOME_UP_PER;
			_distance4 = _stage.stageHeight * ClientConstants.HOME_DOWN_PER;
			_distance2 = _distance3 + _distance4;
		}
		
		public static function get guideDownHeight():int
		{
			return _distance4;
		}
		
		public static function get guideUpHeight():int
		{
			return _distance3;
		}
		
		public static function get guideHeight():int
		{
			return _distance2;
		}
		
		public static function get heightDistance():int
		{
			return _distance1;
		}
		
		public static function get changeFromDPI():Number
		{
			return DPI;
		}
		
		public static function get UpGuideHeight():int
		{
			return _stage.stageHeight * (ClientConstants.HOME_UP_PER + 0.05);
		}
		
		public static function get donwGuideHeight():int
		{
			return _stage.stageHeight * (ClientConstants.HOME_DOWN_PER + 0.05);
		}
		
		public static function get deviceWidth():uint
		{
			if(_stage.stageWidth > 800)
			{
				return 800;
			}else
			{
				return 400;
			}
		}
		
		public static function get deviceHeight():uint
		{
			if(_stage.stageHeight > 1000)
			{
				return 1000;
			}else
			{
				return 600;
			}
		}
		/**
		 *将屏幕宽等切成适应num 的 大小 
		 * @param num
		 * @param itemWid
		 * @return 
		 * 
		 */		
		public static function fromeNumToscale(num:uint,itemWid:Number):Number
		{
			var per:Number = _stage.stageWidth / (num + 1) > itemWid ? itemWid : (_stage.stageWidth / num) / itemWid;
			if(itemWid == per) per = _stage.stageWidth / (num + 1) / itemWid;
			return per * 0.75;
		}
		/**
		 *将target1摆放在target2的中间位置 
		 * @param target1
		 * @param target2
		 * 
		 */		
		public static function toPos(target1:DisplayObject,target2:DisplayObject):void
		{
			target1.x = target2.x + (target2.width - target1.width)/2;
			target1.y = target2.y + (target2.height - target1.height)/2;
		}
		
		/**
		 *将 target 装换成适应 手机屏幕大小的宽度 已长宽中最小为标准 
		 * @param target
		 * 
		 */		
		public static function changeImageScale(target:DisplayObject):void
		{
			var per:Number;
			if(_stage.stageWidth/_stage.stageHeight < target.width/target.height)
			{
				per = target.width / target.height;
				target.height = _stage.stageHeight;
				target.width = per * _stage.stageHeight;
				
			}else
			{
			per = target.height / target.width;
			target.width = _stage.stageWidth;
			target.height = per * _stage.stageWidth;
			target.y = (_stage.stageHeight - target.height)/2;
			}
		}
		
		/**
		 * 
		 * @param scale 显示相对于舞台大小的比例
		 * @param itemWid 显示对象的宽度
		 * @return 显示对象的缩放宽度
		 * 
		 */		
		public static function 	changeToAutosize(scale:Number,itemWid:Number):Number
		{
			return _stage.stageWidth * scale / itemWid;
		}
		
		public static function changeSizeFromWid(target:DisplayObject,wid:int):void
		{
			var oldWid:int = target.width;
			var oldHei:int = target.height;
			target.width = wid;
			target.height = oldHei / oldWid * wid;
		}
		
		public static function changeSizeFromHei(target:DisplayObject,hei:int):void
		{
			var oldWid:int = target.width;
			var oldHei:int = target.height;
			target.height = hei;
			target.width = oldHei / oldWid * hei;
		}
		
		public static function hasMaxValue(scale:Number,maxValue:int):int
		{
			if(_stage.stageHeight * scale > maxValue) return maxValue;
			else return _stage.stageHeight * scale;
		}
		
	}
}